package com.example.yiparkapplication.activity.index.activity.discountCard;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.yiparkapplication.R;
import com.example.yiparkapplication.constant.API;
import com.example.yiparkapplication.entity.MonthCard;
import com.example.yiparkapplication.util.BaseActivity;
import com.example.yiparkapplication.util.Common;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DiscountCardActivity extends BaseActivity implements View.OnClickListener {
    private Handler handler;
    private String mLicensePlate;
    private List<MonthCard> mMonthCardList;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_card_layout);
        init();
    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.id_buy_coupon_card:
                Intent intent = new Intent(DiscountCardActivity.this, SelectDiscountCardsActivity.class);
                intent.putExtra("licensePlate", mLicensePlate);
                startActivity(intent);
                break;
        }
    }

    private void init(){
        handler = new Handler();
        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
        String phoneNumber = pref.getString("phoneNumber", "");
        String userId = pref.getString("userId", "");
        mLicensePlate = pref.getString("licensePlate", "");

        if ("".equals(mLicensePlate)){
            Toast.makeText(getApplicationContext(), "请绑定车辆", Toast.LENGTH_LONG).show();
        }
        RelativeLayout relativeLayout = findViewById(R.id.id_buy_coupon_card);
        relativeLayout.setOnClickListener(this);

        getUserDiscountCard(userId);
    }

    private void getUserDiscountCard(String userId){
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(API.HOST + API.URL_GET_USER_DISCOUNT_CARD + "?licensePlateStr=" + mLicensePlate + "&parkUserId=" + userId)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e){
                Common.failureMessage(e.getMessage(), getApplicationContext());
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException{
                String tempResponse = response.body().string();
                JSONObject joOut = Common.tempResponseException(tempResponse, getApplicationContext());
                int code = joOut.getIntValue("code");
                if (code == 100){
                    JSONArray objects = joOut.getJSONArray("object");
                    if (objects == null){
                        mMonthCardList = new ArrayList<MonthCard>();
                    }else{
                        mMonthCardList = new ArrayList<MonthCard>();
                        for (int i=0; i<objects.size();i++){
                            JSONObject cardInfo = objects.getJSONObject(i);
                            int status = cardInfo.getInteger("status");
                            if (status != 2 && status != 4){
                                MonthCard monthCard = new MonthCard();
                                monthCard.setMonthCardId(cardInfo.getLongValue("monthCardId"));
                                monthCard.setPlateVipId(cardInfo.getInteger("id"));
                                monthCard.setMonthCardName(cardInfo.getString("monthCardName"));
                                monthCard.setLicensePlate(cardInfo.getString("licensePlate"));
                                monthCard.setStreetName(cardInfo.getString("streetName"));
                                monthCard.setStartTime(cardInfo.getString("startTime"));
                                monthCard.setEndTime(cardInfo.getString("endTime"));
                                monthCard.setStatus(status);
                                monthCard.setCheckTime(cardInfo.getString("checkTime"));
                                monthCard.setExplain(cardInfo.getString("explain"));
                                mMonthCardList.add(monthCard);
                            }
                        }
                    }
                    new Thread(){
                        public void run(){
                            handler.post(runnableUi);
                        }
                    }.start();
                }
            }
        });
    }

    Runnable runnableUi = new Runnable(){
      @Override
      public void run(){
          RecyclerView vRecyclerView = findViewById(R.id.id_coupon_card_alive);
          vRecyclerView.setLayoutManager(new LinearLayoutManager(DiscountCardActivity.this));

      }
    };

}
